package com.example.demo.controller.interceptor;


import com.alibaba.fastjson.JSONObject;

import com.example.demo.common.Result;
import com.example.demo.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;



//拦截器,跟过滤器只存在一个即可,还有就是jwt的请求头key为token
//配合webmvcconfig里的拦截器注册使用,该拦截器还未开启
@Slf4j
@Component
public class ProjectInterceptor implements HandlerInterceptor {
    //目标方法执行前执行
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1.获取请求url
        String url = request.getRequestURI();
        log.info("请求的url:{}",url);

        //2.判断请求中是否包含url,如果包含,说明是登录操作,放行
        if (url.contains("login")){
            log.info("登录操作,放行...");
            return true;
        }

        //3.不是登录操作,获取请求头中的令牌(token)
        String jwt = request.getHeader("token");

        //4.判断令牌是否存在,如果不存在,返回错误结果(未登录).
        if(!StringUtils.hasLength(jwt)){
            log.info("请求头token,返回未登录的信息");
            Result error = Result.error("NOT_LOGIN");
            //手动转换 对象--json ---->阿里巴巴fastJSON
            String notLogin = JSONObject.toJSONString(error);
            response.getWriter().write(notLogin);//响应给浏览器
            return false;
        }

        //5.解析token,如果解析失败,返回错误结果(未登录)
        try {
            JwtUtils.parseJWT(jwt);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("解析令牌失败,返回未登录错误信息");
            Result error = Result.error("NOT_LOGIN");
            //手动转换 对象--json ---->阿里巴巴fastJSON
            String notLogin = JSONObject.toJSONString(error);
            response.getWriter().write(notLogin);//响应给浏览器
            return false;
        }

        //6.放行
        log.info("令牌合法,放行");


        return true;//为true才可放行
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
